Thumb

What is ArrayList?

1/8/2020 2:15:07 AM

ArrayList is a collection of data. This list is non-generic data type. We can’t need to assign size of the collection also we can’t set the type. It is automatically type cast by the value. ArrayList is dynamically behaver like objects. ArrayList like loosely typed.  We can also use add method in ArrayList. Now given example code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using testForClass1;

namespace testFor
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList myArrayList = new ArrayList() { 100, 5.3, "jesy", 'k', false };
            foreach (var arrayList in myArrayList)
            {
                Console.WriteLine("Value are : " + arrayList + " ---- Type : " + arrayList.GetType());
            }
            Console.WriteLine();
            Console.WriteLine();
            ArrayList myArrayListAddMethod = new ArrayList();
            myArrayListAddMethod.Add(100);
            myArrayListAddMethod.Add(5.3);
            myArrayListAddMethod.Add("jesy");
            myArrayListAddMethod.Add('k');
            myArrayListAddMethod.Add(false);
            foreach (var arrayList in myArrayListAddMethod)
            {
                Console.WriteLine("Value are : " + arrayList + " ---- Type : " + arrayList.GetType());
            }
            Console.Read();
        }
    }
}

In this code we use the “myArrayList” which is direct assign value and print the value using the foreach loop. Then we use the add method to assign value into “myArrayListAddMethod” and print the value using foreach loop also it’s printed the type of value.